package com.java.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.java.enums.TransTypeEnum;
import com.java.mapper.UserInfoMapper;
import com.java.pojo.bo.TransFlowBO;
import com.java.pojo.entity.TransFlow;
import com.java.mapper.TransFlowMapper;
import com.java.pojo.entity.UserInfo;
import com.java.service.TransFlowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java.service.UserInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 交易流水表 服务实现类
 * </p>
 *
 * @author Helen
 * @since 2021-07-08
 */
@Service
public class TransFlowServiceImpl extends ServiceImpl<TransFlowMapper, TransFlow> implements TransFlowService {

    @Resource
    private UserInfoMapper userInfoMapper;

    @Resource
    private TransFlowMapper transFlowMapper;

    //保存流水
    @Override
    public void saveTransFlow(TransFlowBO transFlowBO) {
        String bindCode = transFlowBO.getBindCode();
        QueryWrapper<UserInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("bind_code",bindCode);
        UserInfo userInfo = userInfoMapper.selectOne(wrapper);
        //保存交易流水
        TransFlow transFlow = new TransFlow();
        transFlow.setTransAmount(transFlowBO.getAmount());
        transFlow.setUserId(userInfo.getId());
        transFlow.setUserName(userInfo.getName());
        transFlow.setTransNo(transFlowBO.getAgentBillNo());
        transFlow.setTransType(TransTypeEnum.RECHARGE.getTransType());
        transFlow.setTransTypeName(transFlowBO.getTransTypeEnum().getTransTypeName());
        transFlow.setMemo(transFlowBO.getMemo());
        transFlowMapper.insert(transFlow);
    }

    @Override
    public Boolean isSaveTransFlow(String agentBillNo) {
        QueryWrapper<TransFlow> wrapper = new QueryWrapper<>();
        wrapper.eq("trans_no",agentBillNo);
        Integer count = transFlowMapper.selectCount(wrapper);
        return count>0;
    }

    @Override
    public List<TransFlow> getList(Long userId) {
        QueryWrapper<TransFlow> wrapper = new QueryWrapper<>();

        wrapper.eq("user_id",userId)
                .orderByDesc("id");
        return transFlowMapper.selectList(wrapper);
    }
}
